package com.learn.flyweight.other;

import com.learn.flyweight.Color;

import java.util.HashMap;

/**
 * <p>项目名称: design-pattern </p>
 *
 * <p>文件名称: OtherChessmanFactory </p>
 *
 * 围棋棋子工厂类：享元工厂类，使用单例模式进行设计
 */
public class OtherChessmanFactory {

    private static OtherChessmanFactory instance;
    private static HashMap<Color, OtherChessman> container;

    private OtherChessmanFactory() {
        // protect against instantiation via reflection
        if (instance == null) {
            instance = this;
        } else {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static synchronized OtherChessmanFactory getInstance() {
        if (instance == null) {
            instance = new OtherChessmanFactory();
            container = new HashMap<>();
            OtherBlackChessman blackChessman = new OtherBlackChessman();
            OtherWhiteChessman whiteChessman = new OtherWhiteChessman();
            container.put(Color.BLACK, blackChessman);
            container.put(Color.WHITE, whiteChessman);
        }

        return instance;
    }

    public OtherChessman getChessman(Color color){
        return container.get(color);
    }


}
